package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.comment.dtos.CommentConfigDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Slf4j
@Transactional
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {

    @Autowired
    private ApArticleService apArticleService;

    /**
     * 修改文章配置信息
     * @param map
     */
    @Override
    public void updateByMap(Map map) {
        //enable 0 下架 1 上架
        Object enable = map.get("enable");
        boolean isDown = true;

        if(enable.equals(1)){
            //判断是上架了
            //isDown 0 没有下架(上架) 1 有下架(下架)
            isDown = false;
            //就是没有下架
        }
        //修改文章
        update(Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId,map.get("articleId"))
                .set(ApArticleConfig::getIsDown,isDown));
    }

    /**
     * (关闭评论后)修改文章评论状态
     * @return
     */
    @Override
    public ResponseResult updateCommentStatus(CommentConfigDto dto) {
        //初始化文章的点赞数量，评论数量等数据
        log.info("修改文章评论状态，初始化文章的点赞数量，评论数量等数据");
        ApArticle apArticle = apArticleService.getById(dto.getArticleId());
        apArticle.setComment(0);
        apArticle.setLikes(0);
//        apArticle.setCollection(0);//收藏数不用初始化
//        apArticle.setViews(0);//阅读数不用初始化
//        apArticleService.save()不会覆盖原有的，会报错
        apArticleService.updateById(apArticle);//更新到数据库

        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,dto.getArticleId())
                //Operation: 0 关闭评论 1 开启评论
                .set(ApArticleConfig::getIsComment,dto.getOperation()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
